package stdio.costa.imoca.notification.sms.babaiyou;

import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.bayou4j.sms.SendSMS;
import stdio.costa.imoca.notification.sms.SMSErrorCode;
import stdio.costa.imoca.notification.sms.SMSResult;
import stdio.costa.imoca.notification.sms.SMSApi;

@Component
public class BabaiyouSMSSender implements SMSApi, InitializingBean {

	private static final Logger logger = LoggerFactory.getLogger(BabaiyouSMSSender.class);
	
	private SendSMS sendSMS;

	@Value("#{properties['babaiyou.username']}")
	private String username;

	@Value("#{properties['babaiyou.password']}")
	private String password;

	@Value("#{properties['babaiyou.sendsms.address']}")
	private String servicesRequestAddress;

	@Override
	public SMSResult sendSMS(String mobile, String content) {

		if (StringUtils.isEmpty(mobile)) {
			return new SMSResult(SMSErrorCode.ErrorCode1,
					SMSErrorCode.ErrorCode1_DESCRIPTION);
		}

		sendSMS.setMessage(content);
		sendSMS.setMobiles(mobile);
		sendSMS.setSmstype(0);
		sendSMS.setTimerid("0");
		sendSMS.setTimertype(0);

		Map<String, String> map = sendSMS.sendSMS();

		String status = map.get("errorcode");
		
		logger.info("error code from babaiyou: {}", status);

		String errorCode = this.convert2SMSErrorCode(status);
		
		return new SMSResult(errorCode, SMSErrorCode.map.get(errorCode));

	}

	public SMSResult sendSMS(List<String> mobileList, String content) {

		if (mobileList == null || mobileList.isEmpty()) {
			return new SMSResult(SMSErrorCode.ErrorCode1,
					SMSErrorCode.ErrorCode1_DESCRIPTION);
		}

		sendSMS.setSmstype(1);
		sendSMS.setTimerid("0");
		sendSMS.setTimertype(0);
		sendSMS.setMessage(content);

		StringBuilder mobilesBuilder = new StringBuilder();

		for (int index = 0; index < mobileList.size() - 1; index++) {
			mobilesBuilder.append(mobileList.get(index));
		}

		mobilesBuilder.append(mobileList.get(mobileList.size() - 1));

		sendSMS.setMobiles(mobilesBuilder.toString());

		Map<String, String> map = sendSMS.sendSMS();

		String status = map.get("errorcode");

		String errorCode = this.convert2SMSErrorCode(status);
		
		return new SMSResult(errorCode, SMSErrorCode.map.get(errorCode));
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		this.sendSMS = new SendSMS();
		this.sendSMS.setUsername(this.username);
		this.sendSMS.setPassword(DigestUtils.md5Hex(this.password));
		this.sendSMS.setServicesRequestAddRess(this.servicesRequestAddress);
	}

	public String convert2SMSErrorCode(String babaiyouErrorCode) {
		if (BabaiyouErrorCode.ErrorCode0.equals(babaiyouErrorCode)) {
			return SMSErrorCode.ErrorCode0;
		}

		if (BabaiyouErrorCode.ErrorCode5.equals(babaiyouErrorCode)) {
			return SMSErrorCode.ErrorCode1;
		}
		
		if (BabaiyouErrorCode.ErrorCode1.equals(babaiyouErrorCode)
				|| BabaiyouErrorCode.ErrorCode2.equals(babaiyouErrorCode)
				|| BabaiyouErrorCode.ErrorCode4.equals(babaiyouErrorCode)
				|| BabaiyouErrorCode.ErrorCode6.equals(babaiyouErrorCode)
				|| BabaiyouErrorCode.ErrorCode7.equals(babaiyouErrorCode)) {
			return SMSErrorCode.ErrorCode2;
		}
		
	    return SMSErrorCode.ErrorCode3;
	}
}
